package com.ruoyi.web.controller.basice_info;

import com.ruoyi.basice_info.domain.TbTeachingResearchSection;
import com.ruoyi.basice_info.service.ITbTeachingResearchSectionService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 教研组设置Controller
 *
 * @author hongyi
 * @date 2023-12-08
 */
@RestController
@RequestMapping("/basice_info/section")
public class TbTeachingResearchSectionController extends BaseController
{
    @Autowired
    private ITbTeachingResearchSectionService tbTeachingResearchSectionService;

    /**
     * 查询教研组设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:section:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbTeachingResearchSection tbTeachingResearchSection)
    {
        startPage();
        List<TbTeachingResearchSection> list = tbTeachingResearchSectionService.selectTbTeachingResearchSectionList(tbTeachingResearchSection);
        return getDataTable(list);
    }

    /**
     * 导出教研组设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:section:export')")
    @Log(title = "教研组设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbTeachingResearchSection tbTeachingResearchSection)
    {
        List<TbTeachingResearchSection> list = tbTeachingResearchSectionService.selectTbTeachingResearchSectionList(tbTeachingResearchSection);
        ExcelUtil<TbTeachingResearchSection> util = new ExcelUtil<TbTeachingResearchSection>(TbTeachingResearchSection.class);
        util.exportExcel(response, list, "教研组设置数据");
    }

    /**
     * 获取教研组设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('basice_info:section:query')")
    @GetMapping(value = "/{teachingResearchSectionId}")
    public AjaxResult getInfo(@PathVariable("teachingResearchSectionId") Long teachingResearchSectionId)
    {

        return success(tbTeachingResearchSectionService.selectTbTeachingResearchSectionByTeachingResearchSectionId(teachingResearchSectionId));
    }

    /**
     * 新增教研组设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:section:add')")
    @Log(title = "教研组设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbTeachingResearchSection tbTeachingResearchSection)
    {
        addOrEdit(tbTeachingResearchSection, "add");
        return toAjax(tbTeachingResearchSectionService.insertTbTeachingResearchSection(tbTeachingResearchSection));
    }

    /**
     * 修改教研组设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:section:edit')")
    @Log(title = "教研组设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbTeachingResearchSection tbTeachingResearchSection)
    {
        addOrEdit(tbTeachingResearchSection, "edit");
        return toAjax(tbTeachingResearchSectionService.updateTbTeachingResearchSection(tbTeachingResearchSection));
    }


    public void addOrEdit(TbTeachingResearchSection tbTeachingResearchSection, String type) {
        TbTeachingResearchSection code = tbTeachingResearchSectionService.getTbTeachingResearchSectionByTeachingResearchSectionCode(tbTeachingResearchSection.getTeachingResearchSectionCode());
        TbTeachingResearchSection name = tbTeachingResearchSectionService.getTbTeachingResearchSectionByTeachingResearchSectionName(tbTeachingResearchSection.getTeachingResearchSectionName());
        if(code!=null) {
           if(type.equals("add")) {
               throw new IllegalArgumentException("教研组代码已经存在");
           } else {
               if(!tbTeachingResearchSection.getTeachingResearchSectionId().equals(code.getTeachingResearchSectionId())) {
                   throw new IllegalArgumentException("教研组代码已经存在");
               }
           }
        }
        if(name!=null) {
            if(type.equals("add")) {
                throw new IllegalArgumentException("教研组名称已经存在");
            } else {
                if(!tbTeachingResearchSection.getTeachingResearchSectionId().equals(name.getTeachingResearchSectionId())) {
                    throw new IllegalArgumentException("教研组名称已经存在");
                }
            }
        }
    }
    /**
     * 删除教研组设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:section:remove')")
    @Log(title = "教研组设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{teachingResearchSectionIds}")
    public AjaxResult remove(@PathVariable Long[] teachingResearchSectionIds)
    {
        return toAjax(tbTeachingResearchSectionService.deleteTbTeachingResearchSectionByTeachingResearchSectionIds(teachingResearchSectionIds));
    }
}
